package com.metronoteapp.adapter;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;
import android.widget.Toast;
import com.metronoteapp.GroupExpActivity;
import com.metronoteapp.R;
import com.metronoteapp.database.GroupNoteDao;
import com.metronoteapp.database.NoteDao;
import com.metronoteapp.dialog.DialogClient;
import com.metronoteapp.dialog.DialogDetailNoteClient;
import com.metronoteapp.dialog.PlayingAudioDialog;
import com.metronoteapp.model.GroupModel;
import com.metronoteapp.model.NoteModel;
import com.metronoteapp.until.Constants;

@SuppressWarnings("unchecked")
public class ExpandGroupNoteAdapter extends BaseExpandableListAdapter {
	public String TAG = ExpandGroupNoteAdapter.class.getSimpleName();
	public LayoutInflater minflater;
	private Context mContext;
	public Activity activity;
	public ArrayList<NoteModel> tempNoteList;
	public GroupModel tempGroupt;
	public ArrayList<Object> mChildtem = new ArrayList<Object>();
	public ArrayList<Object> mGroupItem = new ArrayList<Object>();
	private NoteModel mObjNoteModel;
	private NoteDao mObjNoteDao;
	private GroupNoteDao mObjGroupDao;

	public ExpandGroupNoteAdapter(Context context, ArrayList<Object> grList,
			ArrayList<Object> childItem) {
		this.mGroupItem = grList;
		this.mChildtem = childItem;
		this.mContext = context;
	}

	public void setInflater(LayoutInflater mInflater, Activity act) {
		this.minflater = mInflater;
		activity = act;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return null;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return ((ArrayList<String>) mChildtem.get(groupPosition)).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return null;
	}

	@Override
	public int getGroupCount() {
		return mGroupItem.size();
	}

	@Override
	public void onGroupCollapsed(int groupPosition) {
		super.onGroupCollapsed(groupPosition);
	}

	@Override
	public void onGroupExpanded(int groupPosition) {
		super.onGroupExpanded(groupPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

	@Override
	public View getGroupView(final int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = minflater.inflate(R.layout.layout_group_item, null);
		}
		tempGroupt = (GroupModel) mGroupItem.get(groupPosition);
		Log.v(TAG,
				"getChildView() | " + groupPosition + "|"
						+ tempGroupt.getGroupName());
		CheckedTextView titleGroup = null;
		TextView btnDel = null;
		titleGroup = (CheckedTextView) convertView
				.findViewById(R.id.txtTitleGroupExp);
		titleGroup.setText(tempGroupt.getGroupName());

		btnDel = (TextView) convertView.findViewById(R.id.btnDeleteGroup);

		String strTempColor = tempGroupt.getColor().toString();
		if (strTempColor.equals("Color1")) {
			titleGroup.setBackgroundResource(R.color.Color1);
		} else if (strTempColor.equals("Color2")) {
			titleGroup.setBackgroundResource(R.color.Color2);
		} else if (strTempColor.equals("Color3")) {
			titleGroup.setBackgroundResource(R.color.Color3);
		} else if (strTempColor.equals("Color4")) {
			titleGroup.setBackgroundResource(R.color.Color4);
		} else {
			titleGroup.setBackgroundResource(R.color.Color5);
		}

		titleGroup.setHeight(60);
		titleGroup.setChecked(isExpanded);

		btnDel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				GroupModel tempObj = (GroupModel) mGroupItem.get(groupPosition);
				DialogClient objDialog = new DialogClient(mContext);
				objDialog.displaydialog("Do you really want to delete this group?",
						Constants.TYPE_DEL_GROUP,
						String.valueOf(tempObj.getGroupId()));
			}
		});
		// convertView
		// .setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
		//
		// @Override
		// public void onCreateContextMenu(ContextMenu menu, View v,
		// ContextMenuInfo menuInfo) {
		// // TODO Auto-generated method stub
		//
		// GroupModel tempObj = (GroupModel) mGroupItem
		// .get(groupPosition);
		//
		// String menuItems[] = { "Edit", "Delete" };
		// menu.setHeaderTitle("Option : "
		// + tempObj.getGroupName());
		// for (int i = 0; i < menuItems.length; i++) {
		// menu.add(menu.NONE, i, i, menuItems[i]);
		// }
		// }
		//
		//
		//
		// });
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		tempNoteList = (ArrayList<NoteModel>) mChildtem.get(groupPosition);
		Log.v(TAG,
				"getChildView() | " + groupPosition + "|" + tempNoteList.size());

		TextView title = null;
		TextView content = null;
		TextView start = null;
		TextView icon = null;

		if (convertView == null) {
			convertView = minflater.inflate(R.layout.layout_metro_note_row,
					null);
		}
		title = (TextView) convertView.findViewById(R.id.txtTitle);
		content = (TextView) convertView.findViewById(R.id.txtDetail);
		start = (TextView) convertView.findViewById(R.id.txtStart);
		icon = (TextView) convertView.findViewById(R.id.txtIconTitle);

		title.setText(tempNoteList.get(childPosition).getTitle());
		content.setText(tempNoteList.get(childPosition).getContent());
		if (tempNoteList.get(childPosition).getFlgFav() == 1) {
			start.setBackgroundResource(R.drawable.start_important_select);
		} else {
			start.setBackgroundResource(R.drawable.start_important);
		}

		if (tempNoteList.get(childPosition).getNoteType()
				.equalsIgnoreCase(Constants.TYPE_TEXT)) {
			icon.setBackgroundResource(R.drawable.text_icon);
		} else if (tempNoteList.get(childPosition).getNoteType()
				.equalsIgnoreCase(Constants.TYPE_AUDIO)) {
			icon.setBackgroundResource(R.drawable.voice_icon);
		}

		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				NoteModel tempModelNote = tempNoteList.get(childPosition);
				Log.v(TAG, "Clicked : " + tempModelNote.getTitle());
				Log.v(TAG, "Possition : " + childPosition);
				if (tempModelNote.getNoteType().equalsIgnoreCase(
						Constants.TYPE_TEXT)) {
					DialogDetailNoteClient objDialog = new DialogDetailNoteClient(
							mContext);
					objDialog.displaydialog(
							String.valueOf(tempModelNote.getId()),
							tempModelNote.getTitle(),
							tempModelNote.getContent(),
							String.valueOf(tempModelNote.getFlgFav()),
							tempModelNote.getDateTime());
				} else if (tempModelNote.getNoteType().equalsIgnoreCase(
						Constants.TYPE_AUDIO)) {
					PlayingAudioDialog objPlaying = new PlayingAudioDialog(
							mContext);
					objPlaying.displaydialog(tempModelNote.getContent(),
							tempModelNote.getTitle(),
							tempModelNote.getDateTime());
				}
			}
		});
		return convertView;
	}

	/*
	 * This function parse cursor
	 */
	private static String getColumnAsString(Cursor cursor, int index) {
		return cursor.getString(index).trim();
	}
}
